#!/bin/sh
# dirs
dirs="protected/data protected/runtime assets"
for d in $dirs ; do
  echo "check to create '$d' ..."
  if test -d "$d" ; then
    chmod a+rwx $d
  else
    mkdir -m 0777 $d
  fi
done
# submodule
if test -d ".git" ; then
  git submodule update --init
  git submodule foreach git checkout master
  git submodule foreach git pull
  # create version
  git describe --tags > VERSION
else
  mods="hightman/YiiBooster hightman/yii "
  mods="$mods hightman/yii-EClientScript ddouble/bsie"
  for m in $mods ; do
    d=protected/vendors/`echo $m | cut -d/ -f2`
    if ! test -d "$d" ; then
      git clone git://github.com/${m}.git $d
    elif test -d "$d/.git" ; then
      (cd $d ; git checkout master ; git pull)
    fi
  done
fi
# update latest xunsearch-sdk
wget -q -O .xs-sdk.zip  http://www.xunsearch.com/download/xunsearch-sdk-dev.zip
unzip -q -o .xs-sdk.zip -d protected/vendors
rm -f .xs-sdk.zip
# default app
if ! test -d "protected/data/app" ; then
  echo "creating default app directory ..."
  mkdir -m 0777 protected/data/app
  cp -f protected/vendors/xunsearch-sdk/php/app/demo.ini protected/data/app/demo.ini
  chmod a+rw protected/data/app/demo.ini
else
  chmod a+rwx protected/data/app
  chmod a+rw protected/data/app/*.ini
fi
# db & config
if ! test -f "protected/data/db.sqlite" ; then
  php protected/yiic initdb
fi
chmod a+rw protected/data/db.sqlite
if ! test -f 'protected/config/custom.php' ; then
  cp -f protected/config/custom-sample.php protected/config/custom.php
fi
echo "OK, please modify 'protected/config/custom.php' to your config"
